package cn.itcast.mr.sort;

import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

/**
 * Author itcast
 * Date 2021/10/17 11:25
 * Desc TODO
 */
public class SortPojo implements WritableComparable<SortPojo> {

    private String word;
    private Integer num;

    @Override
    public String toString() {
        return word + "\t" + num;
    }

    public String getWord() {
        return word;
    }

    public void setWord(String word) {
        this.word = word;
    }

    public int getNum() {
        return num;
    }

    public void setNum(Integer num) {
        this.num = num;
    }

    public SortPojo(String word, Integer num) {
        this.word = word;
        this.num = num;
    }

    public SortPojo() {
    }

    /**
     * 根据当前传入的对象指定的字段进行排序
     * 如果 this.字段.compareTo(o.字段) 升序排列
     * 如果 o.字段。compareTo(this.字段) 降序排列
     *
     * @param o
     * @return
     */
    @Override
    public int compareTo(SortPojo o) {
        // this.字段.compareTo(o.字段) 升序排列
        int i = this.word.compareTo(o.word);
        // 如果第一列相同的情况下，第二列降序来排
        if (i == 0) {
            //第二列降序排列
            return o.num.compareTo(this.num);
        } else {
            return i;
        }
    }

    @Override
    public void write(DataOutput out) throws IOException {
        out.writeUTF(word);
        out.writeInt(num);
    }

    @Override
    public void readFields(DataInput in) throws IOException {
        word = in.readUTF();
        num = in.readInt();
    }
}
